/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-通用处理层-任务调度
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.job.converter;

import cn.hutool.core.util.IdUtil;
import com.google.common.collect.ImmutableList;
import com.taiping.cc.application.constant.dao.ConfigStatusEnum;
import com.taiping.cc.application.constant.dao.ParamPositionEnum;
import com.taiping.cc.application.constant.service.PlatformServiceEnum;
import com.taiping.cc.job.dataobject.HttpParamConfigDO;
import com.taipingframework.utility.exception.ManagerException;
import com.taipingframework.utility.http.ServiceConfigInfo;
import com.taipingframework.utility.http.ServiceConfigInfo.ParamProperty;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class HttpParamConfigConverter {

    /**
     * ParamPositionEnum 数据传输对象 转 HttpParamConfigDO 数据对象
     */
    public static HttpParamConfigDO convert(PlatformServiceEnum serviceConstant, ParamPositionEnum position, ParamProperty config) throws ManagerException {
        HttpParamConfigDO paramConfig = new HttpParamConfigDO();
        paramConfig.setParamId(IdUtil.objectId());
        paramConfig.setPlatformCode(serviceConstant.getPlatformCode());
        paramConfig.setPlatformName(null);
        paramConfig.setServiceCode(serviceConstant.getServiceCode());
        paramConfig.setServiceName(null);
        paramConfig.setParamPosition(position.getValue());
        paramConfig.setSourceParamPath(config.getSourceParamPath());
        paramConfig.setSourceParamValue(config.getSourceParamValue());
        paramConfig.setSourceParamType(config.getSourceParamType());
        paramConfig.setTargetFieldName(config.getTargetFieldName());
        paramConfig.setTargetFieldDesc(config.getTargetFieldDesc());
        paramConfig.setTargetFieldClass(null);
        paramConfig.setParamStatus(ConfigStatusEnum.ENABLE.getCode());
        paramConfig.setRemark(null);
        BaseDataObjectConverter.convert(paramConfig);
        return paramConfig;
    }

    /**
     * ParamProperty类型List转HttpParamConfigDO类型List
     */
    public static List<HttpParamConfigDO> convert(List<ParamProperty> propertyList, PlatformServiceEnum serviceConstant, ParamPositionEnum positionEnum) throws ManagerException {
        List<HttpParamConfigDO> configList = new ArrayList<>();
        for (ParamProperty config : propertyList) {
            configList.add(HttpParamConfigConverter.convert(serviceConstant, positionEnum, config));
        }
        return configList;
    }

    /**
     * ServiceConfigInfo 数据传输对象 转 List<HttpParamConfigDO> 数据对象
     */
    public static List<HttpParamConfigDO> convert(PlatformServiceEnum serviceConstant, ServiceConfigInfo serviceConfigInfo) throws ManagerException {
        ServiceConfigInfo.ServiceRequestConfigInfo requestConfigInfo = serviceConfigInfo.getServiceRequestConfigInfo();
        ServiceConfigInfo.ServiceResponseConfigInfo responseConfigInfo = serviceConfigInfo.getServiceResponseConfigInfo();

        // 请求参数配置

        // 地址栏参数列表
        List<HttpParamConfigDO> queryParamConfigList = convert(requestConfigInfo.getQueryParamConfig(), serviceConstant, ParamPositionEnum.QUERY_PARAMS);

        // 请求头参数列表
        List<HttpParamConfigDO> headerParamConfigList = convert(requestConfigInfo.getHeaderParamConfig(), serviceConstant, ParamPositionEnum.REQUEST_HEADER);

        // form表单参数列表
        List<HttpParamConfigDO> formParamConfigList = convert(requestConfigInfo.getFormParamConfig(), serviceConstant, ParamPositionEnum.REQUEST_FORM);

        // JSON报文参数列表(这种情况下，运行时参数使用#占位符#表示)
        List<HttpParamConfigDO> jsonParamConfigList = convert(requestConfigInfo.getJsonParamConfig(), serviceConstant, ParamPositionEnum.REQUEST_JSON);

        // 响应参数配置(http请求通常都是返回json报文，当然如果是文本或者xml，当前版本暂时还不支持，而且，现在我们只解析它的响应体，也暂时先不支持响应头的解析)

        // HTTP响应报文，需要被解析的参数列表
        List<HttpParamConfigDO> mappingParamConfigList = convert(responseConfigInfo.getMappingParamConfig(), serviceConstant, ParamPositionEnum.RESPONSE_JSON);

        // 将 List<List<T>> 转换为 List<T> ，高端操作就是这么秀！啥？你别用 Arrays.asList(T... a) 啊，也别用for循环啊，试试吧，找找看还有没有别的好玩的API，宝贝儿。
        return ImmutableList.of(queryParamConfigList,
                headerParamConfigList, formParamConfigList, jsonParamConfigList, mappingParamConfigList)
                .parallelStream()
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
    }
}
